package edu.chl.da.ejb.persistency;

import edu.chl.da.ejb.core.Airplane;
import edu.chl.da.ejb.core.Seat;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

/**
 * 
 * @author Jakob
 */
@Stateless
public class SeatFacade implements ISeatFacade {
    @PersistenceContext(unitName = "airline_pu")
    private EntityManager em;

    @Override
    public void create(Seat seat) {
        em.persist(seat);
    }

    @Override
    public void edit(Seat seat) {
        em.merge(seat);
    }

    @Override
    public void remove(Seat seat) {
        em.remove(em.merge(seat));
    }

    @Override
    public List<Seat> findAllByAirplane(Airplane a) {
        Query q = em.createQuery("SELECT s FROM Seat s WHERE s.airplane1 = :airplane");
        q.setParameter("airplane", a);
        return q.getResultList();
    }

    @Override
    public List<Seat> findAll() {
        CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Seat.class));
        return em.createQuery(cq).getResultList();
    }

}
